package com.global.college.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.global.college.base.baselist.EventBusBean;
import com.global.college.constants.Constant;
import com.global.college.utils.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.WECHAT_PAY_APP_ID,false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            //支付成功
            if (baseResp.errCode == 0) {
                //支付成功
                ToastUtils.showToastShort("支付成功!");
                EventBus.getDefault().post(new EventBusBean(0, Constant.EventBus.APY_SUCCESS, ""));
                finish();
                //支付取消
            } else if (baseResp.errCode == -2) {
                ToastUtils.showToastShort("支付取消");
                finish();
                //支付失败
            } else {
                EventBus.getDefault().post(new EventBusBean(0, Constant.EventBus.APY_FAIL, ""));
                ToastUtils.showToastShort("支付失败:"+baseResp.errCode+",errStr"+baseResp.errStr+",transaction"+baseResp.transaction);
                finish();
            }
        }
    }


}
